﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

namespace Battle.View
{
    public enum CameraState
    {
        None,
        Enter,   //进场
        Ready,        //等待

        Attack,  //攻击中

        Defend,       //防御 Defender的攻击

        SwitchCamp,      //换阵视角
        Over,      //战斗结束
        Ultimate,  //大招
    }

    public class CameraController
    {
        #region instance
        private static CameraController _instance;
        public static CameraController Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new CameraController();
                return _instance;
            }
        }
        public CameraController()
        {
        }
        #endregion

        private CameraState curState = CameraState.None;

        public void Tick()
        {

        }

        public bool IsCameraTraveling()
        {
            return BattleCamera.Instance.IsTraveling();
        }




        /// <summary>
        /// 摄像机入场
        /// </summary>
        public void Entrance()
        {
            //Debug.Log("Camera Entrance");
            curState = CameraState.Enter;
            BattleCamera.Instance.Reset();
            BattleCamera.Instance.StopAnim();
            BattleCamera.Instance.PlayEnter();
        }

        public void Ready()
        {
            //Debug.Log("Camera Ready");
            switch (curState)
            {
                case CameraState.Enter:
                    break;
                case CameraState.Attack:
                    BattleCamera.Instance.Reset();
                    BattleCamera.Instance.PlayMoveToReady();
                    break;
                case CameraState.Ultimate:
                    BattleCamera.Instance.Reset();
                    BattleCamera.Instance.StopAnim();
                    BattleCamera.Instance.PlayMoveToReady();
                    break;
                case CameraState.SwitchCamp:
                    BattleCamera.Instance.StopAnim();
                    BattleCamera.Instance.PlayMoveToReady();
                    break;
                default:
                    break;
            }
            curState = CameraState.Ready;
        }

        public void SwitchCamp()
        {
            //Debug.Log("Camera SwitchCamp");
            BattleCamera.Instance.PlaySwitchCamp();
            curState = CameraState.SwitchCamp;
        }

        public void Attack(bool isMelee, BattleCamp camp, int fpid)
        {
            //Debug.Log("Camera Attack");
            if (camp == BattleCamp.Attacker)
            {
                switch (curState)
                {
                    case CameraState.Ready:
                    case CameraState.Attack:
                    case CameraState.Ultimate:
                        BattleCamera.Instance.StopAnim();
                        BattleCamera.Instance.Attack();
                        break;
                    default:
                        break;
                }
            }
            else
            {
                switch (curState)
                {
                    case CameraState.Ready:
                        BattleCamera.Instance.StopAnim();
                        BattleCamera.Instance.Defend();
                        break;
                    default:
                        break;
                }
            }
            curState = CameraState.Attack;
        }
        public void ReadyForUltimateEnd(UltimateSkill.UltimateCameraEndType type)
        {
            //Debug.Log("Camera ReadyForUltimateEnd");
            BattleCamera.Instance.Reset();
            BattleCamera.Instance.StopAnim();
            BattleCamera.Instance.PlayReadyForUltimateEnd(type);
        }
        public void UltimateEnd(UltimateSkill.UltimateCameraEndType type)
        {
            //Debug.Log("Camera UltimateEnd");
            BattleCamera.Instance.PlayUltimateEnd(type);

        }
        public void Ultimate(BattleCamp camp, Vector3 pos,float height)
        {
            //Debug.Log("Camera Ultimate");
            switch (curState)
            {
                case CameraState.Ready:
                case CameraState.Attack:
                    BattleCamera.Instance.Reset();
                    BattleCamera.Instance.StopAnim();
                    BattleCamera.Instance.PlayUltimate(camp, pos,height);
                    curState = CameraState.Ultimate;
                    break;
                default:
                    break;
            }

        }

        public void Win()
        {
            //Debug.Log("Camera Win");
            BattleCamera.Instance.Reset();
            BattleCamera.Instance.StopAnim();
            BattleCamera.Instance.PlayWin();
        }
        public void Lose()
        {
            //Debug.Log("Camera Lose");
        }

        public void Shake(int i = 0)
        {
            //Debug.Log("Camera Shake" + " i:" + i);
            BattleCamera.Instance.Shake(i);
        }

        internal void FocusCharactor(BattleCharacter bc, float delay = 0f)
        {
            //Debug.Log("Camera FocusCharactor");
            GameObject obj = bc.model;
            BattleCamera.Instance.SetFocusBattleCharacter(obj, delay);
        }
        internal void UnFocusCharactor(BattleCharacter bc)
        {
            //Debug.Log("Camera UnFocusCharactor");
            GameObject obj = bc.model;
            BattleCamera.Instance.SetUnFocusBattleCharacter(obj);
        }

    }

}